<template>
  <div class="login-body">
    <div class="login-container">
      <div class="head">
        <div class="name">
          <div class="title">MicroCenter</div>
        </div>
      </div>
      <el-form
        v-if="isInit"
        label-position="top"
        :rules="rules"
        :model="ruleForm"
        class="login-form"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            type="text"
            v-model.trim="ruleForm.username"
            @keypress.enter="submitForm"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model.trim="ruleForm.password"
            @keypress.enter="submitForm"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%; margin-top: 30px"
            type="primary"
            @click="submitForm"
            >立即登录</el-button
          >
          <!-- <el-checkbox v-model="checked" @change="!checked"
            >下次自动登录</el-checkbox
          > -->
        </el-form-item>
      </el-form>
      <el-form
        v-else
        label-position="top"
        :rules="rules"
        :model="ruleForm"
        class="login-form"
      >
        <el-form-item label="账号" prop="username">
          <el-input type="text" v-model="root" disabled></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model.trim="ruleForm.password"
            placeholder="请输入初始密码"
            @keypress.enter="submitInitForm"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="rePassword">
          <el-input
            type="password"
            v-model.trim="ruleForm.rePassword"
            placeholder="请再次输入初始密码"
            @keypress.enter="submitInitForm"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%; margin-top: 30px"
            type="primary"
            @click="submitInitForm"
            >初始化密码</el-button
          >
          <!-- <el-checkbox v-model="checked" @change="!checked"
            >下次自动登录</el-checkbox
          > -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, onMounted } from "vue";

export default {
  name: "Login",
  setup() {
    const state = reactive({
      isInit: false,
      root:"root",
      ruleForm: {
        username: "",
        password: "",
        rePassword: "",
      },
      checked: true,
    });

    onMounted(() => {
      $.get("api/micro/isInit").then((r) => {
        state.isInit = r.data;
      });
    });
    return {
      ...toRefs(state),
      submitInitForm(e) {        
        $.post("api/micro/changePassword", {
         ...state.ruleForm,
        }).then((r) => { 
          window.location.reload()
        });
      },
      submitForm(e) {
        $.post("api/micro/login", {
          ...state.ruleForm,
        }).then((r) => {
          $.cache.token = r.data;
          window.location.href = "/";
        });
      },
    };
  },
};
</script>

<style scoped>
.login-body {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  background-color: #fff;
  /* background-image: linear-gradient(25deg, #077f7c, #3aa693, #5ecfaa, #7ffac2); */
}
.login-container {
  width: 420px;
  height: 500px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
}
.head {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0 20px 0;
}
.head img {
  width: 100px;
  height: 100px;
  margin-right: 20px;
}
.head .title {
  font-size: 28px;
  color: #1baeae;
  font-weight: bold;
}
.head .tips {
  font-size: 12px;
  color: #999;
}
.login-form {
  width: 70%;
  margin: 0 auto;
}
</style>
<style>
.el-form--label-top .el-form-item__label {
  padding: 0;
}
.login-form .el-form-item {
  margin-bottom: 12px;
}
</style>
